//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.IO;
using System.Web;
using System.Web.Configuration;
using WCSFContrib.PageFlow.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WCSFContrib.PageFlow.Tests.Utils
{
	[TestClass]
	public class HttpRequestHelperFixture
	{
		[TestInitialize]
		public void Setup()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".aspx" };
		}

		[TestCleanup]
		public void Teardown()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".aspx" };
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void IsAspxPageShouldThrowArgumentNullExceptionWhenRawUrlIsNull()
		{
			HttpRequestHelper.IsHandledByPageHandlerFactory(null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void IsAspxPageShouldThrowArgumentExceptionWhenRawUrlIsEmpty()
		{
			HttpRequestHelper.IsHandledByPageHandlerFactory(string.Empty);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnTrueByDefaultWhenFileExtensionIsAspx()
		{
			string rawUrl = "page.aspx";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);
			
			Assert.IsTrue(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnFalseByDefaultWhenFileExtensionIsNotAspx()
		{
			string rawUrl = "page.xxx";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsFalse(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnTrueWhenRegisteredExtensionFollowedByQueryString()
		{
			string rawUrl = "page.aspx?param1=0";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsTrue(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnTrueWhenUnregisteredExtensionFollowedByQueryString()
		{
			string rawUrl = "page.xxx?param1=0";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsFalse(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnTrueWhenCustomExtensionIsRegisteredAsAnAspxExtension()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".foo" };
			string rawUrl = "page.foo";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsTrue(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnFalseWhenCustomExtensionIsNotRegisteredAsAnAspxExtension()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".foo" };
			string rawUrl = "page.bar";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsFalse(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnFalseWhenExtensionAspxExtensionsAreRegistered()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { };
			string rawUrl = Path.GetRandomFileName();

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsFalse(isAspxPage);
		}

		[TestMethod]
		public void IsAspxPageShouldReturnTrueForAllRegisteredCustomExtensions()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".foo", ".bar" };
			string[] rawUrls = { "page.bar", "page.foo" };

			foreach (string rawUrl in rawUrls)
			{
				bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

				Assert.IsTrue(isAspxPage);
			}
		}

		[TestMethod]
		public void IsAspxPageShouldReturnFalseForNonRegisteredCustomExtensions()
		{
			HttpRequestHelper.ExtensionsHandledByPageHandlerFactory = new string[] { ".foo", ".bar" };
			string[] rawUrls = { "page.xxx", "page.yyy" };

			foreach (string rawUrl in rawUrls)
			{
				bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

				Assert.IsFalse(isAspxPage);
			}
		}

		[TestMethod]
		public void IsAspxPageShouldReturnIgnoreCase()
		{
			string rawUrl = "page.ASPX";

			bool isAspxPage = HttpRequestHelper.IsHandledByPageHandlerFactory(rawUrl);

			Assert.IsTrue(isAspxPage);
		}

		[TestMethod]
		public void SetValidAspxFileExtensionsFromConfigShouldNotSetValidAspxFileExtensionsIfNoHttpHandlerActionsPresent()
		{
			string[] validAspxFileExtensionsBeforeCall = HttpRequestHelper.ExtensionsHandledByPageHandlerFactory;
			HttpHandlersSection httpHandlers = new HttpHandlersSection();

			HttpRequestHelper.LoadExtensionsHandledByPageHandlerFactoryFromConfig(httpHandlers);
			
			Assert.AreSame(validAspxFileExtensionsBeforeCall, HttpRequestHelper.ExtensionsHandledByPageHandlerFactory);
		}

		[TestMethod]
		public void SetValidAspxFileExtensionsFromConfigShouldNotSetValidAspxFileExtensionsIfNoPageHandlerFactoryTypesPresent()
		{
			string[] validAspxFileExtensionsBeforeCall = HttpRequestHelper.ExtensionsHandledByPageHandlerFactory;
			HttpHandlersSection httpHandlers = new HttpHandlersSection();
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.foo", "Foo.Type", "*", false));
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.bar", "Bar.Type", "*", false));

			HttpRequestHelper.LoadExtensionsHandledByPageHandlerFactoryFromConfig(httpHandlers);

			Assert.AreSame(validAspxFileExtensionsBeforeCall, HttpRequestHelper.ExtensionsHandledByPageHandlerFactory);
		}

		[TestMethod]
		public void SetValidAspxFileExtensionsFromConfigShouldIncludeAllPageHandlerFactoryTypes()
		{
			HttpHandlersSection httpHandlers = new HttpHandlersSection();
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.foo", "System.Web.UI.PageHandlerFactory", "*", false));
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.bar", "System.Web.UI.PageHandlerFactory", "*", false));

			HttpRequestHelper.LoadExtensionsHandledByPageHandlerFactoryFromConfig(httpHandlers);

			foreach (HttpHandlerAction httpHandler in httpHandlers.Handlers)
			{
				Assert.IsTrue(HttpRequestHelper.IsHandledByPageHandlerFactory(httpHandler.Path));
			}
		}

		[TestMethod]
		public void SetValidAspxFileExtensionsFromConfigShouldIgnoreNonPageHandlerFactoryTypes()
		{
			HttpHandlersSection httpHandlers = new HttpHandlersSection();
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.foo", "System.Web.UI.PageHandlerFactory", "*", false));
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.bar", "System.Web.UI.PageHandlerFactory", "*", false));
			httpHandlers.Handlers.Add(new HttpHandlerAction("*.aspx", "Some.Other.Type", "*", false));

			HttpRequestHelper.LoadExtensionsHandledByPageHandlerFactoryFromConfig(httpHandlers);
			
			Assert.IsFalse(HttpRequestHelper.IsHandledByPageHandlerFactory("default.aspx"));
		}
	}
}
